/**
 * 
 */
package com.yunhe.tomcat.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;

import com.yunhe.tomcat.iservice.IPoolDao;

/**
 * 数据库连接池
 * 
 * @author 米mi
 *
 */
public class Pool implements IPoolDao {

    /**
     * 链表 --- 实现栈结构
     */
    private LinkedList<Connection> dataSource = new LinkedList<Connection>();

    /**
     * 初始化连接数量
     */
    public Pool() {

        // 一次性创建10个连接
        for (int i = 0; i < Integer.parseInt(DataBaseUtil.maxPoolSize); i++) {
            try {
                // 1.装载mysql驱动
                Class.forName(DataBaseUtil.driver);
                // 2.通过JDBC建立数据库连接
                Connection conn = DriverManager.getConnection(DataBaseUtil.url, DataBaseUtil.username,
                        DataBaseUtil.password);
                // 3.将连接加入连接池中
                dataSource.add(conn);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    /**
     * 取出连接池中一个连接
     * 
     * @return Connection
     */
    public synchronized Connection getConnection() {
        // 删除第一个连接返回
        final Connection conn = dataSource.removeFirst();
        return conn;
    }

    /**
     * 将连接放回连接池
     * 
     * @param conn
     */
    public void releaseConnection(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                dataSource.add(conn);
            }
        }
    }

    /**
     * 创建，回收连接
     * 
     * @param conn
     */
    public void creatConnection(Connection conn) {
    }

    /**
     * 关闭PreparedStatement连接
     * 
     * @param pre
     */
    public void releasePreparedStatement(PreparedStatement pre) {
        if (pre != null) {
            try {
                pre.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    /**
     * 关闭Statement连接
     * 
     * @param st
     */
    public void releaseStatement(Statement st) {
        if (st != null) {
            try {
                st.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    /**
     * 关闭ResultSet连接
     * 
     * @param rs
     */
    public void releaseResultSet(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}
